package com.tamo.ppresta.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.cardview.widget.CardView;

import com.gyf.immersionbar.BarHide;
import com.gyf.immersionbar.ImmersionBar;
import com.tamo.ppresta.R;
import com.tamo.ppresta.base.BaseActivity;
import com.tamo.ppresta.util.ButtonUtils;
import com.tamo.ppresta.util.MyUtils;
import com.tamo.ppresta.util.ThirdApp;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author yeyang
 * @name ppresta
 * @class name：com.tamo.ppresta.activity
 * @class describe
 * @time 2023/1/2 18:42
 * @change
 * @chang time
 * @class 订单关闭
 */
public class OrderAnomalyActivity extends BaseActivity {
    @BindView(R.id.iv_logo)
    ImageView mIvLogo;
    @BindView(R.id.tv_shenhe_tip)
    TextView mTvShenheTip;
    @BindView(R.id.iv_order_state_icon)
    ImageView mIvOrderStateIcon;
    @BindView(R.id.tv_order_state_money1)
    TextView mTvOrderStateMoney1;
    @BindView(R.id.tv_order_state_hint1)
    TextView mTvOrderStateHint1;
    @BindView(R.id.rl_order_state1)
    RelativeLayout mRlOrderState1;
    @BindView(R.id.tv_order_state_money2)
    TextView mTvOrderStateMoney2;
    @BindView(R.id.tv_order_state_hint2)
    TextView mTvOrderStateHint2;
    @BindView(R.id.rl_order_state2)
    RelativeLayout mRlOrderState2;
    @BindView(R.id.tv_order_state_money3)
    TextView mTvOrderStateMoney3;
    @BindView(R.id.tv_order_state_hint3)
    TextView mTvOrderStateHint3;
    @BindView(R.id.rl_order_state3)
    RelativeLayout mRlOrderState3;
    @BindView(R.id.cv1)
    CardView mCv1;
    @BindView(R.id.tv_time)
    TextView mTvTime;
    @BindView(R.id.tv_process_tip)
    TextView mTvProcessTip;
    @BindView(R.id.tv_details)
    TextView mTvDetails;
    @BindView(R.id.view_blue_line)
    View mViewBlueLine;
    @BindView(R.id.view_gray_line)
    View mViewGrayLine;
    @BindView(R.id.view_gray)
    View mViewGray;
    @BindView(R.id.view_blue)
    View mViewBlue;
    @BindView(R.id.rl_line)
    RelativeLayout mRlLine;
    @BindView(R.id.rl_process)
    RelativeLayout mRlProcess;
    @BindView(R.id.tv_look_progress_of_receipt)
    TextView mTvLookProgressOfReceipt;
    @BindView(R.id.cv2)
    CardView mCv2;
    @BindView(R.id.tv_order_tips)
    TextView mTvOrderTips;
    @BindView(R.id.rl_order_tips)
    RelativeLayout mRlOrderTips;
    @BindView(R.id.tv_app_money)
    TextView mTvAppMoney;
    @BindView(R.id.tv_app_money_tip)
    TextView mTvAppMoneyTip;
    @BindView(R.id.tv_app_baifen)
    TextView mTvAppBaifen;
    @BindView(R.id.tv_app_baifen_tip)
    TextView mTvAppBaifenTip;
    @BindView(R.id.ll_app_info)
    LinearLayout mLlAppInfo;
    @BindView(R.id.iv_app_icon)
    ImageView mIvAppIcon;
    @BindView(R.id.tv_app_name)
    TextView mTvAppName;
    @BindView(R.id.rl_app_info)
    RelativeLayout mRlAppInfo;
    @BindView(R.id.tv_descargar_la_app)
    TextView mTvDescargarLaApp;
    @BindView(R.id.view_line1)
    View mViewLine1;
    @BindView(R.id.iv_title_right)
    ImageView mIvTitleRight;
    @BindView(R.id.rl_title_right)
    RelativeLayout mRlTitleRight;
    @BindView(R.id.iv_title_back)
    ImageView mIvTitleBack;
    @BindView(R.id.rl_title_back)
    RelativeLayout mRlTitleBack;

    private PopupWindow mPopupWindow;
    private PopupWindow mProcessPopupWindow;

    @Override
    protected int setLayout() {
        return R.layout.activity_order_anomaly;
    }

    @Override
    protected void setView() {

    }

    @Override
    protected void setData() {
        mRlTitleBack.setOnClickListener(this::onClick);
        mRlTitleRight.setOnClickListener(this::onClick);
        mTvLookProgressOfReceipt.setOnClickListener(this::onClick);
        mTvDescargarLaApp.setOnClickListener(this::onClick);
    }

    @Override
    public void onClick(View v) {
        ButtonUtils.isFastDoubleClick();
        switch (v.getId()) {
            case R.id.rl_title_back:
                finish();
                break;
            case R.id.rl_title_right:
                showKeFuPopWindow();
                break;
            case R.id.tv_look_progress_of_receipt:
                showProcessPopWindow();
                break;
            case R.id.tv_descargar_la_app:
                // TODO: 2023/1/2  下载应用
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        setStatusBar();
    }

    private void setStatusBar() {
        MyUtils.setStatusBarVisible(this);
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .transparentStatusBar()
                .statusBarColor(R.color.color_f3776e)
                .statusBarDarkFont(false)
                .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
                .keyboardEnable(true)
                .init();
    }

    /**
     * 展示客服弹窗
     */
    private void showKeFuPopWindow() {
        // 1、获取用于 PopupWindow 的弹窗view
        LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View customView = layoutInflater.inflate(R.layout.pop_kefutankuang, null);
        View layout = layoutInflater.inflate(R.layout.activity_order_anomaly, null);

        // 2、创建 PopupWindow 实例，传入 View 及宽高
        mPopupWindow = new PopupWindow(customView, RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setOutsideTouchable(false);

        // 3、展示
        mPopupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
        MyUtils.backgroundAlpha(this, 0.5f);

        customView.findViewById(R.id.rl_kefutankuang_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopupWindow.dismiss();
                MyUtils.backgroundAlpha(OrderAnomalyActivity.this, 1f);
            }
        });
        TextView tvPhone1 = customView.findViewById(R.id.tv_phone_number1);
        TextView tvPhone2 = customView.findViewById(R.id.tv_phone_number2);
        customView.findViewById(R.id.iv_wechat1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ThirdApp.chatInWhatsApp(OrderAnomalyActivity.this, tvPhone1.getText().toString());
            }
        });
        customView.findViewById(R.id.iv_phone1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ThirdApp.callPhone(OrderAnomalyActivity.this, tvPhone1.getText().toString());
            }
        });
        customView.findViewById(R.id.iv_wechat2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ThirdApp.chatInWhatsApp(OrderAnomalyActivity.this, tvPhone2.getText().toString());
            }
        });
        customView.findViewById(R.id.iv_phone2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ThirdApp.callPhone(OrderAnomalyActivity.this, tvPhone1.getText().toString());
            }
        });
    }

    private void showProcessPopWindow(){
        // 1、获取用于 PopupWindow 的弹窗view
        LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View customView = layoutInflater.inflate(R.layout.pop_process_tankuang, null);
        View layout = layoutInflater.inflate(R.layout.activity_order_anomaly, null);

        // 2、创建 PopupWindow 实例，传入 View 及宽高
        mProcessPopupWindow = new PopupWindow(customView, RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        mProcessPopupWindow.setOutsideTouchable(false);

        // 3、展示
        mProcessPopupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
        MyUtils.backgroundAlpha(this, 0.5f);


        customView.findViewById(R.id.tv_entiendo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mProcessPopupWindow.dismiss();
                MyUtils.backgroundAlpha(OrderAnomalyActivity.this, 1f);
            }
        });
    }
}